package com.winderinfo.yikaotianxia.ui.login;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.MainActivity;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.kuaijieLogin.LoginBean;
import com.winderinfo.yikaotianxia.ui.register.RegisterActivity;
import com.winderinfo.yikaotianxia.ui.wangjimima.ForgetpwdActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_pwd)
    EditText loginPwd;
    @BindView(R.id.login_login)
    Button loginLogin;
    @BindView(R.id.loging_wangjipwd)
    TextView logingWangjipwd;
    @BindView(R.id.login_register)
    TextView loginRegister;
    private String phone;
    private String pwd;
    private Presenter presenter;
    //创建Map
    Map<String, Object> map = new HashMap<>();

    @Override
    protected int initLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        //实例P
        presenter = new Presenter(this);
    }

    @OnClick({R.id.login_login, R.id.loging_wangjipwd, R.id.login_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_login:
                phone = loginPhone.getText().toString();
                pwd = loginPwd.getText().toString();
                //判断密码，手机号是否为空
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(phone)) {
                    ToastUtil.showWarning(this, "账号或密码不能为空");
                    return;
                }
                //请求登录接口
                /**
                 *@phone 手机号
                 *@password 密码
                 * */
                map.clear();
                map.put("phone", phone);
                map.put("password", pwd);
                presenter.onPostRequest(Api.LOGIN, map, LoginBean.class);
                break;
            case R.id.loging_wangjipwd:
                openActivity(ForgetpwdActivity.class);
                break;
            case R.id.login_register:
                openActivity(RegisterActivity.class);
                break;
        }
    }

    @Override
    public void onSuccess(Object data) {
        //登录
        if (data instanceof LoginBean) {
            //登录失败
            if (((LoginBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, ((LoginBean) data).getMsg());
            }
            //登录成功
            if (((LoginBean) data).getCode() == 0) {
                SPUtils spUtils = new SPUtils(this);
                spUtils.putString("UserID", ((LoginBean) data).getUserId() + "");
                ToastUtil.showSuccess(this, ((LoginBean) data).getMsg());
                openActivityAndCloseThis(MainActivity.class);
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
